#include <stdio.h>
#include <stdlib.h>
#include <GL/glut.h>
#include <iostream>
#include <math.h>

#include "Block.h"
#include "imageloader.h" 

// Material Property
GLfloat block_diffuse[] = {0.50, 0.50, 0.50, 1.0};

//Cuve vertices 
const GLfloat Block::A[3] = { 0.0f, 0.0f, 0.0f }; 
const GLfloat Block::B[3] = { 0.0f, 1.0f, 0.0f }; 
const GLfloat Block::C[3] = { 1.0f, 1.0f, 0.0f }; 
const GLfloat Block::D[3] = { 1.0f, 0.0f, 0.0f }; 
const GLfloat Block::E[3] = { 0.0f, 0.0f, 1.0f }; 
const GLfloat Block::F[3] = { 0.0f, 1.0f, 1.0f }; 
const GLfloat Block::G[3] = { 1.0f, 1.0f, 1.0f }; 
const GLfloat Block::H[3] = { 1.0f, 0.0f, 1.0f }; 


Block::Block(void) {
	blockLength = 1.0;
	blockHeight = 1.0;
	blockWidth = 1.0;
	loadImage();
}

void Block::loadImage()
{
	Image* image1 = loadBMP("textures\\greenMilitary.bmp");
	Image* image2 = loadBMP("textures\\desertMilitary.bmp");
	greenMilitaryTexture = loadTexture(image1);
	desertMilitaryTexture = loadTexture(image2);
	delete image1;
	delete image2;
}

void Block::display() {
	glPushMatrix();
		glPushMatrix();
			glMaterialfv(GL_FRONT, GL_DIFFUSE, block_diffuse);
			//main block
			glScalef(blockLength, blockHeight, blockWidth);
			glColor3f(0.5, 0.5, 0.5);
			glutSolidCube(1.0);
		glPopMatrix();
	glPopMatrix();
}

void Block::greenMilitaryDisplay() {
		glPushMatrix();

			glMaterialfv(GL_FRONT, GL_DIFFUSE, block_diffuse);
			glPushMatrix();
				glBindTexture(GL_TEXTURE_2D, greenMilitaryTexture);
				glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
				glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
			
				//main block
				glScalef(blockLength, blockHeight, blockWidth);
				glColor3f(0.5, 0.5, 0.5);
				cube();
			glPopMatrix();

		glPopMatrix();
}

void Block::desertMilitaryDisplay() {
		glPushMatrix();

			glMaterialfv(GL_FRONT, GL_DIFFUSE, block_diffuse);
			glPushMatrix();
				glBindTexture(GL_TEXTURE_2D, desertMilitaryTexture);
				glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
				glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
			
				//main block
				glScalef(blockLength, blockHeight, blockWidth);
				glColor3f(0.5, 0.5, 0.5);
				cube();
			glPopMatrix();

		glPopMatrix();
}

void Block::cube()
{
	// vertices
	GLfloat A[] = { 0.0f, 1.0f, 1.0f }; 
	GLfloat B[] = { 1.0f, 1.0f, 1.0f }; 
	GLfloat C[] = { 1.0f, 1.0f, 0.0f }; 
	GLfloat D[] = { 0.0f, 1.0f, 0.0f }; 
	GLfloat E[] = { 0.0f, 0.0f, 1.0f }; 
	GLfloat F[] = { 1.0f, 0.0f, 1.0f }; 
	GLfloat G[] = { 1.0f, 0.0f, 0.0f }; 
	GLfloat H[] = { 0.0f, 0.0f, 0.0f }; 

	// texture coordinates
	GLfloat bottomLeft[] = {0.0f, 0.0f};
	GLfloat bottomRight[] = {1.0f, 0.0f};
	GLfloat topLeft[] = {0.0f, 1.0f};
	GLfloat topRight[] = {1.0f, 1.0f};


	glPushMatrix();

		glTranslatef(-0.5, -0.5, -0.5);

		// top
		glBegin(GL_QUADS);
			glNormal3f( 0.0, 1.0, 0.0);
			glTexCoord2fv(bottomLeft);
			glVertex3fv(A);
			glTexCoord2fv(bottomRight);
			glVertex3fv(B);
			glTexCoord2fv(topRight);
			glVertex3fv(C);
			glTexCoord2fv(topLeft);
			glVertex3fv(D);
		glEnd();

		// bottom
		glBegin(GL_QUADS);
			glNormal3f( 0.0, -1.0, 0.0);
			glTexCoord2fv(bottomLeft);
			glVertex3fv(H);
			glTexCoord2fv(bottomRight);
			glVertex3fv(G);
			glTexCoord2fv(topRight);
			glVertex3fv(F);
			glTexCoord2fv(topLeft);
			glVertex3fv(E);
		glEnd();

		// Front
		glBegin(GL_QUADS);
			glNormal3f( 0.0, 0.0, 1.0);
			glTexCoord2fv(bottomLeft);
			glVertex3fv(E);
			glTexCoord2fv(bottomRight);
			glVertex3fv(F);
			glTexCoord2fv(topRight);
			glVertex3fv(B);
			glTexCoord2fv(topLeft);
			glVertex3fv(A);
		glEnd();

		// back
		glBegin(GL_QUADS);
			glNormal3f( 0.0, 0.0, -1.0);
			glTexCoord2fv(bottomLeft);
			glVertex3fv(G);
			glTexCoord2fv(bottomRight);
			glVertex3fv(H);
			glTexCoord2fv(topRight);
			glVertex3fv(D);
			glTexCoord2fv(topLeft);
			glVertex3fv(C);
		glEnd();

		// left
		glBegin(GL_QUADS);
			glNormal3f( -1.0, 0.0, 0.0);
			glTexCoord2fv(bottomLeft);
			glVertex3fv(H);
			glTexCoord2fv(bottomRight);
			glVertex3fv(E);
			glTexCoord2fv(topRight);
			glVertex3fv(A);
			glTexCoord2fv(topLeft);
			glVertex3fv(D);
		glEnd();

		// right
		glBegin(GL_QUADS);
			glNormal3f( 1.0, 0.0, 0.0);
			glTexCoord2fv(bottomLeft);
			glVertex3fv(F);
			glTexCoord2fv(bottomRight);
			glVertex3fv(G);
			glTexCoord2fv(topRight);
			glVertex3fv(C);
			glTexCoord2fv(topLeft);
			glVertex3fv(B);
		glEnd();

	glPopMatrix();
}

Block::~Block(void) {}
	